//
//  PostMessageViewController.m
//  CKApp
//
//  Created by CC on 2020/9/11.
//  Copyright © 2020 This Moment. All rights reserved.
//

#import "PostMessageViewController.h"
#import "LcFoundation.h"
#import "SeparatorCellData.h"
#import "PostContentCellData.h"
#import "PostTagCellData.h"
#import "PostMediaCellData.h"
#import "PostingAuxiliary.h"
#import "TZImagePickerController.h"
#import <Photos/Photos.h>
#import "PostMediaItemCellData.h"
#import "PostAddLocationCellData.h"
#import "SelectLocationViewController.h"

@interface PostMessageViewController () <TZImagePickerControllerDelegate>
@property(nonatomic, weak) IBOutlet UITableView* tableView;
@property(nonatomic, weak) IBOutlet UIButton* bottomButton;
@property(nonatomic, weak) IBOutlet UIView* linerView;
@property(nonatomic, strong) LcCellAdapter* cellAdapter;
@property(nonatomic, strong) PostingAuxiliary* auxiliary;
@property(nonatomic, strong) PostMediaCellData* mediaCellData;
@property(nonatomic, strong) PostAddLocationCellData* locationCellData;
@end

@implementation PostMessageViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self showCloseButton];
  [self setWhiteNavigationBar];
  self.auxiliary = [[PostingAuxiliary alloc] init];
  self.cellAdapter = [[LcCellAdapter alloc] init];
  [self addCellAdapter:self.cellAdapter forTableView:self.tableView];

  [self.bottomButton setBlueStyle];
  [self.bottomButton setBackgroundImage:[UIImage imageWithColor:LC_COLOR_BACKGROUND_BLUE_GRAY_3] forAllState:YES];
  [self.bottomButton setTitle:@"发布"];
  self.bottomButton.layer.cornerRadius = 22.0;

  [self.linerView addVerticalLinearLayer:CGRectMake(0, 0, LcUIUtility.screenWidth, 40.0) colors:@[[UIColor clearColor], LC_COLOR_WHITE]];

  [self refreshAll];
}

- (void)refreshAll {
  [self.cellAdapter removeAllCellDatas];
  LcWeakSelf;
  PostContentCellData* contentCellData = [PostContentCellData cellDataWithContent:self.auxiliary.text doneClickedHandler:^(PostContentCellData* cellData) {
    LcStrongSelf;
    self.auxiliary.text = cellData.text;
  }];
  [self.cellAdapter addCellData:contentCellData];

  SeparatorCellData* separatorCellData10Height = [SeparatorCellData transparentSeparatorDataWithHeight:10.0 showShadow:NO];
  [self.cellAdapter addCellData:separatorCellData10Height];

  if (self.mediaCellData == nil) {
    self.mediaCellData = [PostMediaCellData cellDataMediaMetas:self.auxiliary.mediaMetas deleteClickedHandler:^(__kindof PostMediaItemCellData *cellData) {
      LcStrongSelf;
      [self deleteMedia:cellData];
    } addClickedHandler:^(__kindof CCCollectionViewCellData *cellData) {
      LcStrongSelf;
      [self addMedia];
    }];
  } else {
    self.mediaCellData.mediaMetas = self.auxiliary.mediaMetas;
  }
  [self.cellAdapter addCellData:self.mediaCellData];

  if (self.locationCellData == nil) {
    self.locationCellData = [PostAddLocationCellData cellDataWithTitle:self.auxiliary.poiName selectLocationHandler:^(id sender) {
      LcStrongSelf;
      [self addLocation];
    } clearLocationHandler:^(id sender) {
      LcStrongSelf;
      self.auxiliary.meta = nil;
      self.locationCellData.locationTitle = self.auxiliary.poiName;
      [self.cellAdapter refreshCell:self.locationCellData];
    }];
  } else {
    self.locationCellData.locationTitle = self.auxiliary.poiName;
  }
  [self.cellAdapter addCellData:self.locationCellData];

  PostTagCellData* activityCellData = [PostTagCellData cellDataWithImage:@"icon_event" title:@"活动" content:self.auxiliary.activityContent onCellClickedHandler:^(__kindof LcCellData *cellData) {

  }];
  [self.cellAdapter addCellData:activityCellData];

  SeparatorCellData* separatorCellData = [SeparatorCellData normalSeparatorDataWithLeftRightInsetHeight:1.0 leftInset:18.0 rightInset:18.0 backgroundColor:LC_COLOR_WHITE];
  [self.cellAdapter addCellData:separatorCellData];

  PostTagCellData* tagCellData = [PostTagCellData cellDataWithImage:@"icon_tag" title:@"标签" content:self.auxiliary.tagsContent onCellClickedHandler:^(__kindof LcCellData *cellData) {

  }];
  [self.cellAdapter addCellData:tagCellData];

  SeparatorCellData* separatorCellData40Height = [SeparatorCellData transparentSeparatorDataWithHeight:40.0 showShadow:NO];
  [self.cellAdapter addCellData:separatorCellData40Height];

  [self.cellAdapter refreshAllCells];
}

#pragma mark - Media
- (void)deleteMedia:(PostMediaItemCellData*)cellData {
  CCMediaMeta* meta = cellData.mediaMeta;
  [self.auxiliary deleteMeta:meta];
  [self refreshAll];
}

- (void)addMedia {
  TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 columnNumber:3 delegate:self pushPhotoPickerVc:YES];
  imagePickerVc.selectedAssets = self.auxiliary.assets.mutableCopy;
  imagePickerVc.allowTakePicture = YES;
//  imagePickerVc.videoMaximumDuration = 10; // 视频最大拍摄时间
  [imagePickerVc setUiImagePickerControllerSettingBlock:^(UIImagePickerController *imagePickerController) {
      imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
  }];
  imagePickerVc.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0];
  imagePickerVc.showPhotoCannotSelectLayer = YES;
  imagePickerVc.cannotSelectLayerColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
  [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {

  }];

  imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
  [self presentViewController:imagePickerVc animated:YES completion:nil];
}

#pragma mark - Action
- (void)addLocation {
  LcWeakSelf;
  SelectLocationViewController* viewCtrl = [[SelectLocationViewController alloc] initWithLocationMeta:self.auxiliary.meta selectHandler:^(LocationMeta* meta) {
    LcStrongSelf;
    self.auxiliary.meta = meta;
    self.locationCellData.locationTitle = self.auxiliary.poiName;
    [self.cellAdapter refreshCell:self.locationCellData];
  }];
  [self.navigationController pushViewController:viewCtrl animated:YES];
}

@end
